#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <stack>
#include <string>
using ll = int64_t;

ll n,ans;
std::string s;
const ll maxn = 2e6+5;
ll dp[maxn],end[maxn];

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n;
    std::cin>>s;
    s='\0'+s;
    struct S{
        ll num,pos,ans;
    };
    std::stack<S> stk;
    for(ll i=1;i<=n;i++){
        if(stk.size() && stk.top().num==s[i]){
            if(stk.size()==1){
                end[i]+=end[stk.top().pos-1]*(ans-stk.top().ans);
                ans+=end[i];
            }
            ans+=1;
            end[i]=ans;
            stk.pop();
        }else{
            stk.emplace(s[i],i,ans);
        }
        printf("i=%lld, ans=%lld, end=%lld\n",i,ans,end[i]);
    }
    std::cout<<ans<<"\n";
}